<?php
class CAS
{
    private $param;
    private $numpar;
    private $system;
    private $saida;
    private $arquivo;
    private $cwd;
    public function __construct($system = "scilab")
    {
        $this->system = $system;
        $this->param = array();
        $this->numpar = 0;
        $this->arquivo = date("YmdHis")."_".crc32(rand(1,100000)).".txt";
        $this->addParam("diary('".$this->arquivo."')");
	$this->cwd = "/home/projetos/anaweb/anaweb/sci";
    }
    public function __destruct()
    {
        if (file_exists($this->cwd."/".$this->arquivo))
        {
            //unlink($this->cwd."/".$this->arquivo);
        }
    }
    public function exec()
    {
        global $DOCUMENT_ROOT;
        $descriptorspec = array(
            0 => array("pipe", "r"),
            1 => array("pipe", "w"),
            2 => array("pipe", "w")
        );
        if ($this->system == "scilab")
        {
            $env = array('HOME' => $this->cwd ,'DISPLAY' => '0');
            $process = proc_open('scilab -nogui', $descriptorspec, $pipes, $this->cwd, $env);
            $this->addParam("diary(0)");
        }
        elseif ($this->system == "maple")
        {
            $env = array('HOME' => $this->cwd ,'DISPLAY' => '0', 'LD_ASSUME_KERNEL' => '2.4.1');
            $process = proc_open('/usr/local/maple/bin/maple', $descriptorspec, $pipes, $this->cwd, $env);
        }
        $this->saida='';
        if (is_resource($process)) {
            $str1 = array("%","'");
            $str2 = array("\%","''");
            for ($i=0; $i<count($this->param);$i++)
            {
                if ($this->system == "scilab")
                {
                    fwrite($pipes[0], "printf(' ".str_replace($str1,$str2,$this->param[$i])."\\n');".$this->param[$i]."\n");
                    fflush($pipes[0]);
                    usleep(1000);
                    $op = fread($pipes[1],1024);
                    $this->saida .= $op;
                    if (substr($op,-6) == "more ?")
                        fwrite($pipes[0],"\n");
                    elseif (substr($op,-4) == "halt")
                        fwrite($pipes[0],"\n");
                }
                elseif ($this->system == "maple")
                    fwrite($pipes[0],$param[$i]."\n");
            }
            if ($this->system == "scilab")
                fwrite($pipes[0],"exit\n");
            fclose($pipes[0]);
            fclose($pipes[1]);
            fclose($pipes[2]);
            $return_value = proc_close($process);
        }
    }
    public function disp()
    {
        echo "<pre>";
        echo str_replace(" more ?",'',$this->saida);
        echo "</pre>";
    }
    public function getdet()
    {
        $strings = explode("-->",$this->saida);
        array_pop($strings);
        array_shift($strings);
        foreach ($strings as $s)
        {
            $coms = explode("\n",$s,2);
            $res = explode("=",$coms[1]);
            if (trim($res[0]) == "ans")
                return trim($res[1]);
        }
    }
    public function getgauss()
    {
        /** Abre o arquivo */
        $arquivo = fopen($this->arquivo,"r");
        while (!feof($arquivo))
            $var=@$var.fread($arquivo,1024);
        fclose($arquivo);

        /** Separa a parte da string que contem a resposta
         * de outras saidas */
        $ans = explode("ans  =",$var);
        $ans = explode("-->",$ans[1]);
        $vetx = explode("\n",trim($ans[0]));

        /** Transformo o vetor solucao que e uma string
         * em um numero real, caso seja uma string de erro
         * nao a transformarei */
        for ($i = 0; $i < count($vetx); $i++)
        {
            $tmp = trim(substr($vetx[$i],4,9));
            if (is_numeric($tmp) || empty($tmp))
            {
                $sim = true;
                $vetx[$i] = (double)(str_replace(" ","",$vetx[$i]));
            }
        }

 
        /** Retorno a string de erro, ou entao o vetor solucao */
        //if ((count($vetx) == 1) && !is_numeric($vetx[0]))
        if (!isset($sim))
        {
            foreach($vetx as $v)
                $r=@$r.trim($v);
            return $r;
        }
        else
            foreach($vetx as $v)
                $r=@$r.", $v";
        return "(".substr($r,2).")<sup>T</sup>";
    }
    public function addParam($param)
    {
        $this->param[$this->numpar++] = $param;
    }
    public function loadMatrix($matrix,$varname)
    {
        $l = count($matrix);
        for ($i = 0 ; $i < $l; $i++)
        {
            if ($i != 0) $str .= "; ";
            for ($j = 0; $j < count($matrix[$i]); $j++)
            {
                $str = @$str.$matrix[$i][$j]." ";
            }
        }
        $this->addParam($varname." = [ $str ]");
    }
}
?>
